beansieve 是一个用于整理 beancount 账本的小工具。这个工具主要实现以下两个功能:
- 通过
--type aggregate将账单中包含特定账户的条目放到一个新账本文件中。 - 通过
--type archive将特定时间范围内的 Transaction 条目保留到一个新账本文件中。
项目可以在 Github 上查看。
使用 pip install beansieve 进行安装,并通过 python -m beansieve 使用,具体用法参考下面的例子。
一些术语
type:操作类型,即aggregate按账户拆分账本或archive按时间范围归档账本;source:源文件,即要操作的账本文件;dest:目标文件,即新生成的账本文件的保存目录;rule:使用aggregate时用于指定聚合规则;keep:使用archive时用于指定保留时间范围。
Structure 输出文件的结构
运行该工具后会使用 include 重新组织账本文件,组织规则如下:
── account.beancount # 账户相关指令,即 `open` 和 `close`
├── commodity.beancount # commodity 指令
├── custom.beancount # custom 指令(如 fava 中提供的 balance 指令)
├── document.beancount # document 指令
├── event.beancount # event 指令
├── history.beancount # 如果使用 `archive`,则 `keep` 范围外的 Transaction 会被移动到此文件夹中
├── main.beancount # 主账本文件,include 其他账本文件,如果使用 `archive` 则会包含 `keep` 范围内的 Transaction,如果使用 `aggregate` 则未匹配上的 Transaction 文件会被移动到此文件
├── note.beancount # note 指令
├── price.beancount # price 指令
├── <key>.beancount # 使用 `aggregate` 时按 `rule` 拆分出来的账本
└── query.beancount # query 指令
aggregate 的用法
使用 aggregate 将账单中包含特定账户的条目放到一个新账本文件中。
通过使用 --rule 设置账户匹配规则,支持设置多种规则,单条规则的格式为 <key>:<pattern>,其中 <key> 为文件名称,<pattern> 为一个用于匹配账户名称的正则表达式。如果有多条规则则使用 , 分割匹配规则。
例如下面的脚本会将 :
posting中账户名称匹配Income:US:ETrade:.*的 Transaction 移动到Etrade.beancount文件;posting中账户名称匹配Income:US:Babble:Salary的 Transaction 移动到Payroll.beancount文件;- 其他的
Transaction移动到main.beancount文件; - 其他的
Directive会按照Structure规则移动到特定的文件中。
# generate sample beancount files
bean-example > .artifacts/example.beancount
bean-report .artifacts/example.beancount balances > .artifacts/example-balances.txt
# aggregate
rm -rf .artifacts/example_aggregate
python -m beansieve \
--type aggregate \
--source ".artifacts/example.beancount" \
--dest ".artifacts/example_aggregate" \
--rule "Etrade|Income:US:ETrade:.*,Payroll|Income:US:Babble:Salary"
bean-report .artifacts/example_aggregate/main.beancount balances > .artifacts/example_aggregate-balances.txt
archive 的用法
使用 archive 能够保留将特定时间范围内的 Transaction 条目移动到 main.beancount 文件中,时间范围外的 Transaction 会被移动到 history.beancount 文件中。
其中 --keep 用于指定从当前时刻其回溯的时间范围,比如 1d 表示自当前时刻起前 1 天的 Transaction 会被保留到 main.beancount 文件中,1w 则保留前 7 天的条目,1m 保留前 30 天的条目。
例如下面的脚本会将:
- 从当前时刻起前 7 天的
Transaction移动到main.beancount文件; - 其他
Transaction移动到history.beancount文件; - 其他的
Directive会按照Structure规则移动到特定的文件中。
# generate sample beancount files
bean-example > .artifacts/example.beancount
bean-report .artifacts/example.beancount balances > .artifacts/example-balances.txt
rm -rf .artifacts/example_archive
python -m beansieve \
--type archive \
--source ".artifacts/example.beancount" \
--dest ".artifacts/example_archive" \
--keep 7d
bean-report .artifacts/example_archive/main.beancount balances > .artifacts/example_archive-balances.txt
注意
- 这个工具不能保证输入(
--source)文件结构的完整性,因此如果有使用include拆分账本文件的习惯,请先确保你认同Structure的拆分规则; aggregate和archive这两个命令不能同时使用。对我而言只是当有拆分Transaction的时候才会使用aggregate拆分账本,更多时候是通过archive来确保最近一段时间的账本文件(main.beancount)足够精简;- 使用前做好备份,以防止数据丢失,在使用前后使用
bean-report生成balances报表进行对比,可以确保转换无误。